layui.use(['jquery','layer','table','form'], function(){
    var $=layui.jquery ,form = layui.form,layer=layui.layer,table = layui.table;
    var tableList = table.render({
        elem: '#list'
        ,height: 'auto'
        ,url: '/manage/Role/roleList' //数据接口
        ,method:'post'
        ,page: true //开启分页
        ,cols: [[ //表头
            {field: 'id', title: 'ID',  sort: true, fixed: 'left'}
            ,{field: 'title', title: '用户组名称'}
            ,{ title: '创建者',align:"center"}
            ,{field: 'created_at', title: '添加时间'}
            ,{field: 'updated_at', title: '修改时间'}
            ,{ title: '操作',toolbar: '#action',align:"center" }
        ]]
    });
    $('.layuiadmin-btn-role').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //事件
    var active = {
        batchdel: function(){
            var checkStatus = table.checkStatus('LAY-user-back-role')
                ,checkData = checkStatus.data; //得到选中的数据
            if(checkData.length === 0){
                return layer.msg('请选择数据');
            }

        },
        addRole: function(){
            var roleid =$(this).data('id');
            layer.open({
                type: 1,
                title: '添加角色'
                ,content: $("#add_role_from")
                ,offset: 'auto'
                ,area: ['400px', '200px']
                ,btn: ['确定', '取消']
                ,btnAlign: 'c' //按钮居中
                ,closeBtn :1
                ,yes: function(i){
                   var rolename =  $("#rolename").val();
                    $.post("/manage/Role/addRole",{
                        rolename:rolename,
                        roleid : roleid
                    },function(data){
                        if(data.code == 0){
                            $("#rolename").val("");
                            layer.alert(data.message,function(){
                                table.reload('list');
                                layer.closeAll();
                            })
                        }else{
                            layer.msg(data.message)
                        }
                    });

                    // layer.closeAll();
                }
            });
        }
    }

    //监听工具条
    table.on('tool(list)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        console.log(data);

        if(layEvent === 'rule'){ //权限
            //do somehing
        } else if(layEvent === 'del'){ //删除
            layer.confirm('真的删除行么', function(index){
                $.post("/manage/Role/delRole",{
                    roleid:data.id
                },function(data){
                    if(data.code == 0){
                        layer.alert(data.message,function(index){
                            // table.reload('list');
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);
                        })
                    }else{
                        layer.msg(data.message)
                    }
                });

                //向服务端发送删除指令
            });
        } else if(layEvent === 'edit'){ //编辑
            //do something
            $("#rolename").val(data.title);
            var type = "addRole";
            active[type] ? active[type].call(this) : '';

            //同步更新缓存对应的值
            // obj.update({
            //     title: data.
            // });
        }
    });

    // loading =layer.load(1, {shade: [0.1,'#fff']});
    // layer.close(loading);
});

